package main

import (
	"context"
	"fmt"
	service "grpc_study/proto"
	"net"

	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials"
)

// 这里用继承的方式
type SayHelloServer struct {
	service.UnimplementedSayHelloServer
}

func (s *SayHelloServer) SayHello(ctx context.Context, req *service.HelloRequest) (*service.HelloResponse, error) {
	fmt.Println("hello " + req.RequestName)
	return &service.HelloResponse{
		ResponseMessage: "hello " + req.RequestName,
	}, nil
}

func main() {
	// 指定证书和key
	// 两个参数分别是：自签名证书文件、私钥文件
	creds, _ := credentials.NewServerTLSFromFile("D:\\dev\\golang\\grpc\\kuangshen\\code\\01_grpc_study\\secure\\test.pem", "D:\\dev\\golang\\grpc\\kuangshen\\code\\01_grpc_study\\secure\\test.key")
	// 开启端口
	lis, err := net.Listen("tcp", ":9000")
	if err != nil {
		panic(err)
	}
	defer lis.Close()
	// 创建grpc服务
	// 创建服务时将证书传入
	grpcServer := grpc.NewServer(grpc.Creds(creds))
	// 将自己的服务注册到grpc服务中
	service.RegisterSayHelloServer(grpcServer, &SayHelloServer{})
	// 启动服务
	err = grpcServer.Serve(lis)
	if err != nil {
		panic(err)
	}
}
